﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Xml;
using CommandLine;
using CommandLine.Text;

namespace GitUtil
{
    class Program
    {
        private static Process proc = null;
        private static HashSet<String> chanageFileList;
        public class Options
        {
            [Option("c1", Required = true, HelpText = "Git提交ID（1）")]
            public String Commit1 { get; set; }

            [Option("c2", Required = true, HelpText = "Git提交ID（2）")]
            public String Commit2 { get; set; }

            [Option('r',"root", Required = true, HelpText = "项目根路径")]
            public String RootPath { get; set; }
        }

        private static readonly HeadingInfo HeadingInfo = new HeadingInfo("GIT增量工具", "V1.0.0");
        static void Main(string[] args)
        {
            Parser.Default.ParseArguments<Options>(args).WithParsed(Run);
        }

        private static void Run(Options option)
        {
            Console.WriteLine($"\r\n对比提交差异： {option.Commit1} {option.Commit2}");

            var git = new CommandRunner("git", option.RootPath);

            var commnd = $"diff {option.Commit1} {option.Commit2} --name-only";
            Console.WriteLine($"执行命令 git {commnd}");

            var diffFiles = git.Run(commnd);
            Console.WriteLine($"差异文件：");
            Console.WriteLine(diffFiles);

            PhraseChangeList(diffFiles);

            Console.WriteLine("\r\n执行打包命令");

            //var mvn = new CommandRunner("mvn.bat", option.RootPath);
            //var mvnHelp = mvn.Run("clean");
            //Console.WriteLine(mvnHelp);

            Console.WriteLine("\r\n解析项目模块");
            ModuleInfo moduleInfo = new PomParseUtil().parsePom(option.RootPath, null);

            //解析变更文件归属模块和输出文件名
            foreach (String fileName in chanageFileList)
            {
                Console.WriteLine("\r\n解析文件{0}", fileName);
                String preFileName = fileName.Split("src")[0];
                String belongModule = preFileName.Split('/')[1];
                Console.WriteLine("归属模块{0}", belongModule);

                if(moduleInfo != null)
                {
                    ModuleInfo moduleInfoNode = FindModuleByName(moduleInfo, belongModule);

                    if (fileName.EndsWith(".java"))
                    {
                        String filePath = fileName.Substring(fileName.IndexOf("src/main/java/"));
                        String fileRealPath = filePath.Replace("src/main/java/", "").Replace(".java", ".class");
                        Console.WriteLine("解析到java类文件路径为{0}", fileRealPath);

                        moduleInfoNode.AddClassFile(fileRealPath);
                    }
                    else
                    {
                        moduleInfoNode.AddResourcesFilePath(fileName);
                    }

                }
            }

            Console.WriteLine("\r\n按 Enter 退出程序……");
            Console.ReadLine();
        }



        private static ModuleInfo FindModuleByName(ModuleInfo moduleInfo, String moduleName)
        {
            if(moduleInfo == null)
            {
                return null;
            }

            if (moduleInfo.name.Equals(moduleName))
            {
                return moduleInfo;
            }
            else
            {
                if(moduleInfo.childModules != null)
                {
                    foreach(ModuleInfo m in moduleInfo.childModules)
                    {
                        ModuleInfo outModule = FindModuleByName(m, moduleName);
                        if(outModule != null)
                        {
                            return outModule;
                        }
                    }

                }
            }

            return null;
        }

        private static void PhraseChangeList(String res) {
            if (res != null) {
                string[] list = res.Split(new char[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries);

                if (list!=null && list.Length>0) {
                    chanageFileList = new HashSet<string>(list.Length);
                    foreach (string s in list) {
                        chanageFileList.Add(s);
                    }
                }
            }
        }
    }
}
